import { register, login } from '@/api/user'
import { getToken, setToken } from '@/utils/storage'
import { getUserInfoAPI } from '@/api/index'
// import { Message } from 'element-ui'
export default {
  namespaced: true,
  state () {
    return {
      token: getToken() || '',
      user: {}
    }
  },
  mutations: {
    setUserToken (state, newToken) {
      state.token = newToken
      // 持久化一份到本地
      setToken(newToken)
    },
    setUser (state, userinfo) {
      state.user = userinfo
    },
    setAvatar (state, avatar) {
      state.user.avatar = avatar
    }
  },
  actions: {
    async registerEvent (context, obj) {
      await register(obj)
    },
    async loginEvent (context, obj) {
      const data = await login(obj)
      context.commit('setUserToken', data.token)
    },
    async getUser (context) {
      const res = await getUserInfoAPI() // 请求获取数据
      context.commit('setUser', res.data)
    }
  },
  getters: {}
}
